﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Threading;
using com.skype.api;

namespace Hugo
{
    class SignInMgr
    {
        public static String MY_CLASS_TAG = "SignInMgr";
        public static int DELAY_INTERVAL = 1000;   // Polling interval equivalent to 1 second.
        public static int DELAY_CNT = 45;  // Request polling limit (iterations).
        // Results in a maximum total delay of DELAY_CNT * DELAY_INTERVAL milliseconds before giving up and failing.
        public static int LOGOUT_DELAY = (1 * 1000);   // Delay interval prior to logout (milliseconds). Equivalent to 1 second.

        // Common SkypeKit login processing.
        public Boolean Login(MySession mySession, String myAccountPword)
        {

            if (mySession.isLoggedIn())
            {
                // Already logged in...
                return (true);
            }

            // Issue login request
            mySession.myAccount.loginWithPassword(myAccountPword, false, true);

            // Loop until AccountListener shows we are logged in or time-out...
            int i = 0;
            while ((i < SignInMgr.DELAY_CNT) && (!mySession.isLoggedIn()))
            {
                Thread.Sleep(SignInMgr.DELAY_INTERVAL);
            }

            if (i < SignInMgr.DELAY_CNT)
            {
                // Successful Login
                return (true);
            }
            else
            {
                return (false);
            }
        }


        // Common SkypeKit  logout processing.
        public void Logout(MySession mySession)
        {

            // Give the runtime a chance to catch its breath if it needs to...
            Thread.Sleep(SignInMgr.LOGOUT_DELAY);

            if (!mySession.isLoggedIn())
            {
                return;
            }

            // Issue logout request
            mySession.myAccount.logout(false);

            // Loop until AccountListener shows we are logged out or we time-out...
            int i = 0;
            /*
             * 		while ((i < SignInMgr.DELAY_CNT) && (SignInMgr.isLoggedIn(mySession.myAccount))) {
             */
            while ((i < SignInMgr.DELAY_CNT) && (mySession.isLoggedIn()))
            {
                Thread.Sleep(SignInMgr.DELAY_INTERVAL);
            }

            if (i < SignInMgr.DELAY_CNT)
            {
                // Successful Logout
            }
            else
            {
                Console.WriteLine("Logout timed out for " + mySession.myAccountName + "! (IP Addr " + MySession.IP_ADDR + ":" + MySession.PORT_NUM);
            }
        }


        // Dynamically determines if an Account is signed in.

        public static Boolean isLoggedIn(com.skype.api.Account myAccount)
        {

            if (myAccount != null)
            {
                if (myAccount.getStatusWithProgress().status == Account.Status.LOGGED_IN)
                {
                    return (true);
                }
            }
            return (false);
        }
    }
}
